using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microcharts
{
    /// <summary>
    /// Base class for chart managing Series
    /// </summary>
    public abstract class SeriesChart : Chart
    {
        /// <summary>
        /// internal series
        /// </summary>
        protected IEnumerable<ChartSerie> series;

        /// <summary>
        /// Gets or sets the Series
        /// </summary>
        /// <value>IEnumerable of <seealso cref="T:Microcharts.ChartSerie"/></value>
        public IEnumerable<ChartSerie> Series
        {
            get => series;
            set => UpdateSeries(value);
        }

        /// <summary>
        /// Method to update <see cref="T:Microcharts.SeriesChart.series"/> property.
        /// </summary>
        /// <param name="value">IEnumerable of <seealso cref="T:Microcharts.ChartSerie"/> to set.</param>
        protected virtual void UpdateSeries(IEnumerable<ChartSerie> value)
        {
            Set(ref series, value);
            UpdateEntries(series.SelectMany(s => s.Entries).ToList());
        }

        /// <summary>
        /// Gets or Sets Entries
        /// </summary>
        /// <remarks>When entries is set, a default series of this entries is generated</remarks>
        /// <value>IEnumerable of <seealso cref="T:Microcharts.ChartEntry"/></value>
        public IEnumerable<ChartEntry> Entries
        {
            get => entries;
            set => GenerateDefaultSerie(value);
        }

        /// <summary>
        /// Indicate if the content of Series was auto-generated (from Entries)
        /// </summary>
        /// <value>Boolean indicate if Series was auto-generated</value>
        protected bool IsSeriesAutoGenerated { get; private set; }

        /// <summary>
        /// Method called when Entries is set, for generate Series from Entries.
        /// </summary>
        /// <param name="value"></param>
        protected virtual void GenerateDefaultSerie(IEnumerable<ChartEntry> value)
        {
            UpdateSeries(new ChartSerie[]
            {
                new ChartSerie()
                {
                    Entries = value.ToList(),
                    Name = "Default - AutoGenerated",
                    Color = null
                }
            });
            IsSeriesAutoGenerated = true;
        }
    }
}
